Exercise 1

library(ggplot2)
library(tibble)

# Load the mpg dataset
data(mpg)

# Create a scatterplot with customized aesthetics
ggplot(mpg, aes(x = cty, y = hwy, color = manufacturer, size = displ)) +
  geom_point(size = 2) + 
  theme_void() + 
  theme()

Exercise 2

polar_art <- function(seed, n, palette) {
  
  # set the state of the random number generator
  set.seed(seed)
  
  # data frame containing random values for 
  # aesthetics to use in the art
  dat <- tibble(
    x0 = runif(n),
    y0 = runif(n),
    x1 = x0 + runif(n, min = -.2, max = .2),
    y1 = y0 + runif(n, min = -.2, max = .2),
    shade = runif(n), 
    size = runif(n)
  )
  
  # plot segments in various colours, using 
  # polar coordinates and a gradient palette
  dat |> 
    ggplot(aes(
      x = x0,
      y = y0,
      xend = x1,
      yend = y1,
      colour = shade,
      size = size
    )) +
    geom_segment(show.legend = FALSE) +
    coord_polar() +
    scale_y_continuous(expand = c(0, 0)) +
    scale_x_continuous(expand = c(0, 0)) + 
    scale_colour_gradientn(colours = palette) + 
    scale_size(range = c(0, 10)) + 
    theme_void()
}

# Define a fun palette
fun_palette <- c("#FF4081", "#FFC107", "#8BC34A", "#03A9F4", "#9C27B0")

# Set the seed for reproducibility
seed <- 7829

# Generate a piece using the fun palette
polar_art(seed, n = 200, palette = fun_palette)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

polar_art <- function(seed, n, palette) {
  set.seed(seed)  # Set the seed for reproducibility
  
  # Generate random coordinates for the polar art
  x <- runif(n, min = -1, max = 1)
  y <- runif(n, min = -1, max = 1)
  
  # Plot the polar art
  plot(x, y,
       col = palette,
       pch = 19,
       xlim = c(-1, 1), ylim = c(-1, 1),
       xlab = "", ylab = "",
       main = NA,
       axes = FALSE)
}


# Save the modified polar_art() function in the "polar_art_02.R" file
file_path <- "polar_art_02.R"
writeLines(deparse(polar_art), file_path)


# Load the modified polar_art() function from the "polar_art_02.R" file
source("polar_art_02.R")

# Generate and display the custom polar art with a random seed and custom palette
seed <- sample(1:1000, 1)  # Randomly select a seed from 1 to 1000

polar_art(seed, n = 7000, palette = fun_palette)

We might be naming the function as a second version in an art context because it we are going for random art, and if we forget which function does which design in the plot, that just adds to the randomness and beauty of the function as a whole.

Exercise 3

# Define the sample_canva() function
sample_canva <- function() {
  # Generate a sample color palette
  palette <- c("#FF4081", "#536DFE", "#FFD740", "#1DE9B6", "#9C27B0")
  return(palette)
}

# Generate a color palette using sample_canva()
pal_ette <- sample_canva()
sample_named_colours <- function(n) {
  # Get the set of distinct named colours
  all_colours <- colours(distinct = TRUE)
  
  # Sample n distinct colours
  sampled_colours <- sample(all_colours, size = n)
  
  return(sampled_colours)
}

# Define the polar_art() function
polar_art <- function(seed, n, color_palette) {
  set.seed(seed)  # Set the seed for reproducibility
  
  # Generate random coordinates for the polar art
  x <- runif(n, min = -1, max = 1)
  y <- runif(n, min = -1, max = 1)
  
  # Plot the polar art using ggplot2
  data <- data.frame(x = x, y = y, color = color_palette)
  
  ggplot(data, aes(x = x, y = y)) +
    geom_point(size = 3, shape = 16, aes(color = color)) +
    scale_color_identity() +
    theme_void()
}

# Set the seed for reproducibility
seed <- 123

# Generate a random palette of 5 named colors
num_colors <- 5
color_palette <- sample_named_colours(num_colors)

# Generate and display the polar art using the random palette
polar_art(seed, n = 500, color_palette = color_palette)

sample_canva_palette <- function(n) {
  # Get the set of distinct colors from ggthemes::canva_palettes
  all_colors <- unlist(ggthemes::canva_palettes)
  
  # Sample n distinct colors randomly
  sampled_colors <- sample(all_colors, size = n)
  
  return(sampled_colors)
}

#Generate a random palette of 5 colors
num_colors <- 5
random_palette <- sample_canva_palette(num_colors)
print(random_palette)
##         Modern and muted1 Subdued and proffesional4                Nightlife2 
##                 "#a9b7c0"                 "#763626"                 "#ff0038" 
##                  Coastal2         Earthy and fresh1 
##                 "#f3d3b8"                 "#945d60"

Exercise 3

#Copying functions due to R not cooperating

sample_data <- function(seed = NULL, n = 100){
  if(!is.null(seed)) set.seed(seed)
  dat <- tibble(
    x0 = runif(n),
    y0 = runif(n),
    x1 = x0 + runif(n, min = -.2, max = .2),
    y1 = y0 + runif(n, min = -.2, max = .2),
    shade = runif(n), 
    size = runif(n),
    shape = factor(sample(0:22, size = n, replace = TRUE))
  )
}

polar_styled_plot <- function(data = NULL, palette) {
  ggplot(
    data = data,
    mapping = aes(
      x = x0,
      y = y0,
      xend = x1,
      yend = y1,
      colour = shade,
      size = size
    )) + 
    coord_polar(clip = "off") +
    scale_y_continuous(
      expand = c(0, 0),
      limits = c(0, 1), 
      oob = scales::oob_keep
    ) +
    scale_x_continuous(
      expand = c(0, 0), 
      limits = c(0, 1), 
      oob = scales::oob_keep
    ) + 
    scale_colour_gradientn(colours = palette) + 
    scale_size(range = c(0, 10)) + 
    theme_void() + 
    guides(
      colour = guide_none(),
      size = guide_none(),
      fill = guide_none(),
      shape = guide_none()
    )
}
library(ggplot2)

# Generate random data
set.seed(123)
data <- sample_data(seed = 123, n = 200)

# Visualization using ggplot2
ggplot(data, aes(x = x0, y = y0, size = size, fill = shade)) +
  geom_point(shape = 21, color = "black") +
  scale_size_continuous(range = c(1, 10)) +
  scale_fill_gradient(low = "blue", high = "red") +
  coord_polar(theta = "x", start = 0) +
  labs(title = "", x = "", y = "") +
  theme_void()+
  theme(legend.position = "none")

Task 2 Doing it for spatial tricks Loading in packages

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(purrr)
library(tibble)
library(ggplot2)
library(ggthemes)
library(ambient)
# Define canvas
x_coords <- seq(from = 0, to = 1, length.out = 800)
y_coords <- seq(from = 0, to = 1, length.out = 800)
canvas <- long_grid(x = x_coords, y = y_coords) 

# Generate artwork using gen_perlin() with different values
set.seed(1234)

# Default gen_perlin() output
canvas <- canvas %>%
  mutate(paint = gen_perlin(x, y, frequency = 10, seed = 1234))
art_default <- ggplot(canvas, aes(x, y, fill = paint)) +
  geom_raster(show.legend = FALSE)

# Altered gen_perlin() output with different frequency
canvas <- canvas %>%
  mutate(paint = gen_perlin(x, y, frequency = 1, seed = 1234))
art_frequency <- ggplot(canvas, aes(x, y, fill = paint)) +
  geom_raster(show.legend = FALSE)

# Altered gen_perlin() output with different seed
canvas <- canvas %>%
  mutate(paint = gen_perlin(x, y, frequency = 10, seed = 5678))
art_seed <- ggplot(canvas, aes(x, y, fill = paint)) +
  geom_raster(show.legend = FALSE)

# Plot the artworks
art_default

art_frequency

art_seed

make_noise_art <- function(
    generator = gen_perlin, 
    frequency = 10, 
    seed = 1234,
    pixels = 2000,
    palette = c("#e5ddc8", "#01949a", "#004369", "#db1f48"), 
    ...
) {
  
  # define the grid
  canvas <- long_grid(
    x = seq(from = 0, to = 1, length.out = pixels),
    y = seq(from = 0, to = 1, length.out = pixels)
  ) 
  
  # use the generator to add paint
  canvas <- canvas |>
    mutate(
      paint = generator(
        x, y, 
        frequency = frequency, 
        seed = seed, 
        ...
      )
    )
  
  # use ggplot2 to draw the picture
  art <- canvas |> 
    ggplot(aes(x, y, fill = paint)) + 
    geom_raster(show.legend = FALSE) +
    theme_void() +
    coord_equal() +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0)) +
    scale_fill_gradientn(colours = palette)
  
  return(art)
}

# Generate noise art with default arguments
make_noise_art()

# Vary the seed
make_noise_art(seed = 123)

make_noise_art(seed = 568)

# Vary the frequency
make_noise_art(frequency = 10)

make_noise_art(frequency = 20)

# Vary the palette
make_noise_art(palette = c("white", "black"))

# Vary the generator
make_noise_art(generator = gen_perlin)

make_noise_art(generator = gen_worley)

make_noise_art(generator = gen_waves)

# Define the blank canvas
blank_canvas <- long_grid(
  x = seq(from = 0, to = 1, length.out = 2000),
  y = seq(from = 0, to = 1, length.out = 2000)
)

# Define a plotting function
plot_painted_canvas <- function(canvas, palette = NULL) {
  if (is.null(palette)) {
    palette <- c("#e5ddc8", "#01949a", "#004369", "#db1f48")
  }
  canvas %>%
    ggplot(aes(x, y, fill = paint)) +
    geom_raster(show.legend = FALSE) +
    theme_void() +
    coord_equal() +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(expand = c(0, 0)) +
    scale_fill_gradientn(colours = palette)
}

# Create a spatial noise pattern
pattern_canvas <- blank_canvas %>%
  mutate(
    noise1 = gen_perlin(x, y, frequency = 10),
    noise2 = gen_perlin(x, y, frequency = 30),
    noise3 = gen_perlin(x, y, frequency = 50),
    paint = (noise1 + noise2 + noise3) / 3
  )

# Plot the spatial noise pattern
plot_painted_canvas(pattern_canvas)

fractal_art <- function(fractal, generator, palette = NULL, ...) {
  blank_canvas |>
    mutate(
      paint = fracture(
        noise = generator,
        fractal = fractal,
        x = x, 
        y = y, 
        ...
      )
    ) |>
    plot_painted_canvas(palette = palette)
}


fractal_art(billow, gen_worley, seed = 5, octaves = 4)

fractal_art(billow, gen_worley, seed = 11, octaves = 11)

fractal_art(billow, gen_worley, seed = 2, octaves = 2)

#Putting it all together I’m gonna make a smiling face

# Define eye colors
eye_colors <- c(
  "amber"   = "#FFC300",   # Vibrant Amber
  "blue"    = "#00A8FF",   # Vibrant Blue
  "brown"   = "#A0522D",   # Vibrant Brown
  "gray"    = "#808080",   # Gray
  "green"   = "#00FF00",   # Vibrant Green
  "hazel"   = "#FFA500"    # Vibrant Hazel
)

# Generate a piece using the fun palette
Left <- fractal_art(fbm, gen_simplex, freq_init = 0.01, octaves = 15, seed = 1738, palette = eye_colors)

Right <- fractal_art(fbm, gen_simplex, freq_init = 0.01, octaves = 15, seed = 4269, palette = eye_colors)

# Define lip colors
lip_colors <- c(
  "nude"     = "#E6B8A2",   # Nude
  "pink"     = "#FFB6C1",   # Pink
  "red"      = "#FF0000",   # Red
  "berry"    = "#800080",   # Berry
  "coral"    = "#FF7F50",   # Coral
  "plum"     = "#DDA0DD"    # Plum
)

make_noise_art <- function(generator) {
  fractal_art(fbm, generator, freq_init = 0.01, octaves = 15, seed = 5, palette = lip_colors)
}

Curl <- make_noise_art(gen_waves)
Brush <- make_noise_art(generator = gen_perlin)

#skin tones palette
skin_tones <- c(
  "olive"           = "#B3A28F",
  "medium_tan"      = "#D3B18B",
  "warm_beige"      = "#E3C8A1",
  "golden_brown"    = "#D6A679",
  "rich_brown"      = "#9D7B5A",
  "deep_caramel"    = "#7C5B4E"
)

# Create a spatial noise pattern
pattern_canvas <- blank_canvas %>%
  mutate(
    noise1 = gen_perlin(x, y, frequency = 13),
    noise2 = gen_perlin(x, y, frequency = 35),
    noise3 = gen_perlin(x, y, frequency = 55),
    paint = (noise1 + noise2 + noise3) / 3
  )

# Plot the spatial noise pattern using the skin_tones palette
Face <- plot_painted_canvas(pattern_canvas, palette = skin_tones)
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggthemes':
## 
##     theme_map
# Create the plot objects
plot_face <- ggdraw() + draw_plot(Face) + theme(plot.margin = margin(0))
plot_left <- ggdraw() + draw_plot(Left) + theme(plot.margin = margin(0))
plot_right <- ggdraw() + draw_plot(Right) + theme(plot.margin = margin(0))
plot_curl <- ggdraw() + draw_plot(Curl) + theme(plot.margin = margin(0))
plot_brush <- ggdraw() + draw_plot(Brush) + theme(plot.margin = margin(0))

# Arrange the plots in a grid with reduced vertical spacing between columns
grid <- plot_grid(
  plot_face, plot_face, plot_face, plot_face, plot_face, plot_face,
  plot_face, plot_left, plot_face, plot_face, plot_right, plot_face,
  plot_face, plot_face, plot_face, plot_face, plot_face, plot_face,
  plot_face, plot_curl, plot_brush, plot_brush, plot_curl, plot_face,
  plot_face, plot_curl, plot_brush, plot_brush, plot_curl, plot_face,
  plot_face, plot_face, plot_face, plot_face, plot_face, plot_face,
  nrow = 6, ncol = 6,
  rel_heights = c(1, 0.8, 1, 1, 0.8, 1)  # Adjust the relative heights
)

# Display the grid
grid

#Reflection

I enjoyed exploring how code in R can be used to enhance artistic capabilities. It was fascinating to see the different ways in which coding techniques can create beautiful visual designs. However, personally, I prefer physically drawing with a pen because I like the feeling of it. Multimedia art using code was unfamiliar and a little scary at first. But in the end, it was fun to experiment with different types of plots and visualizations. While I still prefer traditional art forms, I appreciate the precision and complexity that coding can bring to artistic compositions. Overall, it was a valuable experience that reminded me to embrace new technologies and continue evolving as an artist. Playing with the random aspects of it is very fun as it added a layer of variablitity I wasnt used to.